#!/bin/bash
# configure Makefile for climail

# locate python bin
PYTHON=`which python`
LINEPERSCREEN=30

# read arguments...
while [ -n "$1" ]; do
	case $1 in
		--skip-mbox)
			SKIPMBOX=1
		;;

		--lines-per-once)
			if [ -n $2 ]; then
				LINEPERSCREEN=$2
				shift
			fi
		;;

		--help)
			echo -e "--skip-mbox                skip creating mbox file if already exists in destination directory"
			echo -e "--lines-per-once <int>     skip creating mbox file if already exists in destination directory"
			echo -e "--help                     show this usage information"
			exit 0
		;;

		*)
			echo -ne "Skip option $1, wrong\n" 2> /dev/null
		;;
	esac
	shift
done;


echo -ne "bin python located... $PYTHON\n"


# locate python's libs
for item in `whereis python`; do
	case $item in
		/usr/lib/*)
			PYLIBS="$item"
			break;
		;;
	esac
done

if [ -d "$PYLIBS/site-packages" ]; then
	PYLIBS="$PYLIBS/site-packages"
	echo -ne "python's libs located at... $PYLIBS\n"
else
	echo -ne "python's libs was not located!\n"
	exit 1
fi

# screen heigth
:

# chk for metamail
echo -ne 'Checking for metamail utility... '
MMAIL="`which metamail`" 2> /dev/null

if [ $? -eq 0 ]; then
	echo -ne "$MMAIL\n"
else
	echo -ne '< FAIL >\n'
	exit 1
fi

# chk for modules...
echo -ne 'Checking for python modules installed... '
grep -s '^ *\(import\|from\) ' bin/climail* | grep -v '\(import\|from\) pmArgs' | sed 's/#.*//;s/.*://;s/^ \+//' | sort -u | python

if [ $? -eq 0 ]; then
	echo -ne '<  OK  >\n'
else
	echo -ne '< FAIL >\n'
	exit 1
fi

# create mbox
mkdir -m 700 .climail &> /dev/null && mkdir -m 700 .climail/profiles &> /dev/null && mkdir -m 700 .climail/bodies &> /dev/null

#if [ $? -ne 0 ]; then
#	exit 1
#fi

if [ -z "$SKIPMBOX" -a ! -f "$HOME/.climail/mbox" ]; then
	echo -ne "<mb:MBox xmlns:mb=`grep '^NS_URI' usr/lib/python/site-packages/pmArgs.py | sed 's/NS_URI *= *//'` />" > .climail/mbox
fi

if [ ! -z $LINEPERSCREEN ]; then
	# SCREEN_H     = 30
	sed "s/^SCREEN_H *= *[0-9]\+/SCREEN_H     = $LINEPERSCREEN/" bin/climail > climail
fi

# read home dir
HDIR="$HOME"

# read bindir
BINDIR='/usr/bin'
MANDIR='/usr/share/man'
DOCDIR='/usr/share/doc'

# create Makefile
(
echo -ne '# climail Makefile:\n'

# all target
echo -ne 'all: climail\n'
echo -ne 'climail: bin/climail bin/climailadd bin/climaildel bin/climailmod bin/climailget\n\n'

# install target
echo -ne 'install: bin/climail bin/climailadd bin/climaildel bin/climailmod bin/climailget '
echo -ne 'usr/lib/python/site-packages/pmArgs.py '
echo -ne 'usr/share/man/man1/climail.1.gz usr/share/man/man1/climailadd.1.gz  usr/share/man/man1/climaildel.1.gz '
echo -ne 'usr/share/man/man1/climailget.1.gz usr/share/man/man1/climailmod.1.gz usr/share/doc/packages/climail/INSTALL '
echo -ne 'usr/share/doc/packages/climail/README.txt usr/share/doc/packages/climail/DEPS\n\t'
echo -ne "install -D -m 555 climail bin/climailadd bin/climaildel bin/climailmod bin/climailget $BINDIR/\n\t"
echo -ne "install -m 444 usr/lib/python/site-packages/pmArgs.py $PYLIBS/\n\t"
echo -ne "install -m 644 usr/share/man/man1/climail.1.gz usr/share/man/man1/climailadd.1.gz usr/share/man/man1/climaildel.1.gz usr/share/man/man1/climailget.1.gz usr/share/man/man1/climailmod.1.gz $MANDIR/man1/\n\t"
echo -ne "if [ ! -d $DOCDIR/packages/climail ]; then mkdir -m 755 $DOCDIR/packages/climail; fi;\n\t"
echo -ne "install -D -m 644 usr/share/doc/packages/climail/INSTALL usr/share/doc/packages/climail/README.txt usr/share/doc/packages/climail/DEPS $DOCDIR/packages/climail/\n\t"
echo -ne "cp -Rp .climail $HDIR/\n\n"

# clean target
echo -ne 'clean:\n\t'
echo -ne 'rm -rf .climail\n\t'
echo -ne 'rm -f climail\n\n'

# uninstall target
echo -ne 'uninstall:\n\t'
echo -ne "rm -rf $DOCDIR/packages/climail\n\t"
echo -ne "rm -f  $MANDIR/man1/climail.1.gz $MANDIR/man1/climailadd.1.gz $MANDIR/man1/climaildel.1.gz\
                 $MANDIR/man1/climailget.1.gz $MANDIR/man1/climailmod.1.gz\n\t"
echo -ne "rm -f  $BINDIR/climail $BINDIR/climailadd $BINDIR/climaildel $BINDIR/climailmod $BINDIR/climailget\n\t"
echo -ne "rm -f  $PYLIBS/pmArgs.py\n\t"
echo -ne "rm -rf $HDIR/.climail\n\n" ) > Makefile

# exit OK
exit 0
